<template>
  <div class="login_container">
    <div class="login_box">
      <!-- logo去 -->
      <div class="avatar_box">
        <img src="../assets/logo.jpg" alt="logo" />
        <span>海报设计系统</span>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            prefix-icon="el-icon-unlock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="doSubmit()"
            >登录</el-button
          >
        </el-form-item>
        <el-row style="text-align: center; margin-top: -10px">
          <el-link type="primary" @click="register">没有账号？点击注册</el-link>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import users from '@/assets/data/users.json';

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
      },
    }
  },
  methods: {
  register() { // 跳转到注册界面
    this.$router.push('/register')
  }, 
  doSubmit() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const user = users.find(user => user.username === this.loginForm.username && user.password === this.loginForm.password);
          if (user) {
            console.log("登录成功");
            // 执行登录逻辑
          } else {
            console.log("账号或密码错误");
            this.$message.error("账号或密码错误");
          }
        } else {
          console.log("表单验证失败");
        }
      });
  }
}
</script>

<style scoped>
.login_container {
  background-image: url("../assets/img.jpg");
  /* 设置图片大小 */
  background-size: 100% 100%;
  /* 设置div高度 */
  height: 100%;
}

.login_box {
  width: 450px;
  height: 400px;
  background-color: white;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  overflow: hidden; /* 隐藏溢出部分 */
}

.avatar_box {
  display: flex; /* 使用 flexbox 布局 */
  align-items: center; /* 垂直居中对齐 */
  justify-content: center; /* 水平居中对齐 */
  height: 100px;
  width: 100%; /*填满父容器宽度 */
  /* box-shadow: 0 0 10px #ddd; */
  padding: 10px 0;
}

.avatar_box img {
  height: 80px;
  width: 80px;
  border-radius: 50%;
  background-color: #eee;
  margin-right: 5px; /*图片与文字之间的间距 */
}

.avatar_box span {
  font-size: 24px; /* 调整文字大小，根据需要调整 */
  color: #333; /* 调整文字颜色，根据需要调整 */
}

.login-form {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 10px 35px 15px 35px;
  background: #fff;
  text-align: left;
}
</style>
